/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mainpackage;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Administrator
 */
public class LogoCanvas extends Canvas implements Runnable {

    private int byUiState;
    private int[] iArray;
    private boolean bFlag;
    private int[] cloud;
    private String[] sArray;
    private Image[] imgArray;
    private Thread aThread;

    public LogoCanvas() {
        this.setFullScreenMode(true);
        initLogo();
    }

    public void start() {
        if (this.aThread == null || (this.aThread != null && !this.aThread.isAlive())) {
            this.aThread = new Thread(this);
            this.aThread.start();
        }
    }

    /**
     * 初始化加载公司logo
     */
    public void initLogo() {
        this.byUiState = 0;
        this.bFlag = false;
        this.byUiState = 0;

        //帧计数数组
        this.iArray = new int[]{0, 255, 0, 0};
        cloud = new int[]{-120, -180, -240, -120, -180, 240, 420};
        this.sArray = new String[]{"读取中…"};
        try {
            //#if NOKIA_S60V5
             // 图片资源
             this.imgArray = new Image[7];
             this.imgArray[0] = ResManager.createImage(90000035);//一门楼游戏logo
             this.imgArray[1] = Tools.createPicFromGrayscale(ResManager.createImage(90000019), -1);            //四个角落的阴影图片
             this.imgArray[2] = Tools.makeTransparent(this.imgArray[0], 0);//公司logo的遮罩层，产生淡入淡出的效果
             this.imgArray[3] = ResManager.createImage(90000020);//游戏封面图片
             this.imgArray[4] = Tools.makeTransparent(Constant.DRAW_WIDTH, 20, 255, 0xFFFFFF);//游戏封面渐变图片
             this.imgArray[5] = ResManager.createImage(90000074);
            //#if QUDAOID == "DANGLE"
             logostate = 0;
             this.imgArray[6] = ResManager.createImage(90000212);
            //#elif QUDAOID == "JIUYOU"
             logostate = 0;
             this.imgArray[6] = ResManager.createImage(90000211);
            //#elif QUDAOID == "UC"
             logostate = 0;
             this.imgArray[6] = ResManager.createImage(90000213);
            //#else
             logostate = 1;
            //#endif
            //#else 
//#            //图片资源
//#            this.imgArray = new Image[9];
//#            //ui图片
//#            this.imgArray[0] = createImage(90000035);//90000035
//#            //四个角落的阴影图片
//#            this.imgArray[1] = createPicFromGrayscale(createImage(90000019), -1);
//#            //公司logo的遮罩层，产生淡入淡出的效果
//#            this.imgArray[2] = makeTransparent(this.imgArray[0], 0);
//#            //游戏封面图片
//#            this.imgArray[3] = createImage(90000026);
//#            //游戏封面渐变图片
//#//            this.imgArray[4] = makeTransparent(Constant.DRAW_WIDTH, 20, 255, 0xFFFFFF);
//#            //游戏logo的渐变图片
//#            this.imgArray[5] = createImage(90000073);
            //#if NORMAL_240_400
//#             imgArray[5] = Tools.ZoomImage(imgArray[5], Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            //#endif
//#            this.imgArray[6] = makeTransparent(this.imgArray[5], 0);
            //#if NOKIA_S40V3 || NORMAL_240_400 || MOT0 || K790
            //#else
//#            this.imgArray[7] = createImage(90000074);
            //#endif
            //#if QUDAOID == "DANGLE"
//#             logostate = 0;
//#             this.imgArray[8] = ResManager.createImage(90000212);
            //#elif QUDAOID == "JIUYOU"
//#             logostate = 0;
//#             this.imgArray[8] = ResManager.createImage(90000211);
            //#elif QUDAOID == "UC"
//#             logostate = 0;
//#             this.imgArray[8] = ResManager.createImage(90000213);
            //#else
//#            logostate = 1;
            //#endif
            //#endif
        } catch (Exception e) {
            e.printStackTrace();
        }
        blrungame = true;
    }
    /**
     * 更新logo加载界面
     */
    int index = 0;//mainFrame = new MainCanvas();
    boolean blShowLoading = false;

    public void updateLogo() {
        if (index != -1) {
            index += 1;
            if (index == 20) {
                blShowLoading = true;
            }
            if (index > 20 && MainGame.mainFrame == null) {
                MainGame.mainFrame = new MainCanvas();
            }
        }
        if (index > 20000) {
            index = 0;
        }
        if (logostate > 0) {
            if (this.iArray[0] < 255) {
                this.iArray[0] = this.iArray[0] + 20 > 255 ? 255 : this.iArray[0] + 20;
                this.imgArray[2] = makeTransparent(this.imgArray[0], this.iArray[0]);
            } else if (this.iArray[1] > 0) {
                this.iArray[1] = this.iArray[1] - 20 > 0 ? this.iArray[1] - 20 : 0;
                this.imgArray[2] = makeTransparent(this.imgArray[0], this.iArray[1]);
                if (this.iArray[1] == 0) {
                    this.byUiState = 0;
                    iArray = new int[]{255, 0, 0, 0};
                    blrungame = false;
                    clearLogo();
                    Display.getDisplay(MainGame.instance).setCurrent(MainGame.mainFrame);
                    MainGame.mainFrame.start();
                    clearLogo();
                }
            }
        }
    }
    /**
     * 绘制logo界面
     * @param g
     */
    public int W, H;
    public int logostate;
    public int logo_index;

    public void drawLogo(Graphics g) {
        if (logostate == 0) {
            //#if QUDAOID == "DANGLE"
//#             g.setColor(0xffffff);
//#             g.fillRect(Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            //#elif QUDAOID == "JIUYOU"
//#             g.setColor(0xf68538);
//#             g.fillRect(Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            //#elif QUDAOID == "UC"
//#             g.setColor(0xffffff);
//#             g.fillRect(Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            //#else
            g.setColor(0xffffff);
            g.fillRect(Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            //#endif
            if (this.imgArray != null) {
                //#if NOKIA_S60V5
                 if (this.imgArray[6] != null) {
                     g.drawImage(this.imgArray[6], Constant.UI_START_X + (Constant.DRAW_WIDTH - this.imgArray[6].getWidth()) / 2,
                             Constant.UI_START_Y + (Constant.DRAW_HEIGHT - this.imgArray[6].getHeight()) / 2, 0);
                     logo_index++;
                     if (logo_index > 30) {
                         logostate = 1;
                     }
                 }
                //#else
//#                if (this.imgArray[8] != null) {
                    //#if NOKIA_E72
                    //#if QUDAOID == "JIUYOU"
//#                     g.drawImage(this.imgArray[8], Constant.UI_START_X + (Constant.DRAW_WIDTH - this.imgArray[8].getWidth()) / 2,
//#                             Constant.UI_START_Y, 0);
                    //#else
//#                     g.drawImage(this.imgArray[8], Constant.UI_START_X + (Constant.DRAW_WIDTH - this.imgArray[8].getWidth()) / 2,
//#                             Constant.UI_START_Y + (Constant.DRAW_HEIGHT - this.imgArray[8].getHeight()) / 2, 0);
                    //#endif
                    //#else
//#                    g.drawImage(this.imgArray[8], Constant.UI_START_X + (Constant.DRAW_WIDTH - this.imgArray[8].getWidth()) / 2,
//#                            Constant.UI_START_Y + (Constant.DRAW_HEIGHT - this.imgArray[8].getHeight()) / 2, 0);
                    //#endif
//#                    logo_index++;
//#                    if (logo_index > 20) {
//#                        logostate = 1;
//#                    }
//#                }
                //#endif
            }

        } else {
            g.setColor(0xffffff);
            g.fillRect(Constant.UI_START_X, Constant.UI_START_Y, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
            if (this.imgArray != null) {
                //#if NOKIA_S40V3 || NORMAL_240_400 || MOT0 || K790
//#                 if (this.imgArray[0] != null) {
//#                     g.drawImage(this.imgArray[0], Constant.UI_START_X + ((Constant.DRAW_WIDTH - this.imgArray[0].getWidth()) >> 1),
//#                             Constant.UI_START_Y + ((Constant.DRAW_HEIGHT - this.imgArray[0].getHeight()) >> 1), 0);
//#                 }
                //#else
                if (this.imgArray[2] != null) {
                    g.drawImage(this.imgArray[2], Constant.UI_START_X + ((Constant.DRAW_WIDTH - this.imgArray[2].getWidth()) >> 1),
                            Constant.UI_START_Y + ((Constant.DRAW_HEIGHT - this.imgArray[2].getHeight()) >> 1), 0);
                }
                //#endif
            }
        }
    }

    public void clearLogo() {
        this.imgArray = null;
        this.iArray = null;
        cloud = null;
        this.sArray = null;
        ResManager.clearRes(Constant.RES_PUBLIC_TEMP);
    }

    protected void paint(Graphics g) {
        g.setColor(0xffffff);
        g.fillRect(0, 0, Constant.DRAW_WIDTH, Constant.DRAW_HEIGHT);
        drawLogo(g);
    }
    private boolean blrungame = false;

    public void run() {
        try {
            while (blrungame) {
                updateLogo();
                repaint();
            }
        } catch (Exception e) {
        }
    }

    public static Image createImage(int imgid) {
        try {
            return Image.createImage(Constant.RES_UI_PATH + imgid + ".png");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Image makeTransparent(Image img, int alpha) {
        int length = img.getWidth() * img.getHeight();
        int[] imgData = new int[length];
        int width = img.getWidth();
        int height = img.getHeight();
        img.getRGB(imgData, 0, img.getWidth(), 0, 0, img.getWidth(), img.getHeight());
        img = null;
        for (int i = 0; i < imgData.length; i++) {
            imgData[i] &= 0x00FFFFFF;
            if (imgData[i] != 0x00ff00ff) {
                imgData[i] = imgData[i] | (alpha << 24);
            }
        }
        return Image.createRGBImage(imgData, width, height, true);
    }

    public static Image makeTransparent(int w, int h, int alpha, int color) {
        int length = w * h;
        int[] imageData = new int[length];
        for (int i = 0; i < length; i++) {
            imageData[i] = (alpha << 24) + color;
        }
        return Image.createRGBImage(imageData, w, h, true);
    }

    public static Image createPicFromGrayscale(Image image, int rgb) {
        int width = image.getWidth();
        int height = image.getHeight();
        int length = width * height;
        int[] imageData;
        imageData = new int[length];
        image.getRGB(imageData, 0, width, 0, 0, width, height);
        image = null;
        int temp;
        if (rgb == -1) {
            for (int i = 0; i < length; i++) {
                //获得透明度
                temp = imageData[i] & 0x000000FF;
                imageData[i] = (imageData[i] & 0x00ffffff) | (temp << 24);
            }
        } else {
            rgb = rgb & 0x00ffffff;
            for (int i = 0; i < length; i++) {
                //获得透明度
                temp = imageData[i] & 0x000000FF;
                imageData[i] = rgb | (temp << 24);
            }
        }
        return Image.createRGBImage(imageData, width, height, true);
    }

//    public static void drawCorner(Graphics g, int iUiX, int iUiY, Image img) {
//        //#if NOKIA_S40V3 || NORMAL_240_400|| MOT0 || K790
////#         return;
//        //#else
//        int w = img.getWidth();
//        int h = img.getHeight();
//        g.drawImage(img, iUiX, iUiY, g.LEFT | g.TOP);
//        g.drawRegion(img, 0, 0, w, h, Constant.TRANS_MIRROR, iUiX + Constant.SCREEN_WIDTH - w, iUiY, 0);
//        g.drawRegion(img, 0, 0, w, h, Constant.TRANS_ROT180, iUiX + Constant.SCREEN_WIDTH - w, iUiY + Constant.SCREEN_HEIGHT - h, 0);
//        g.drawRegion(img, 0, 0, w, h, Constant.TRANS_MIRROR_ROT180, iUiX, iUiY + Constant.SCREEN_HEIGHT - h, 0);
//        //#endif
//    }
    public static void drawTilePic(Graphics g, byte direction, int startX, int startY, int borderLength, Image srcImg, int imgStartX, int imgStartY, int imgWidth, int imgHeight, int transform) {
        //初始开始的位置，用于判断是否超过了
        if (borderLength < 0) {
            return;
        }
        int iOriginalStartX = startX;
        int iOriginalStartY = startY;
        int tempBorderH; // 防止图片重叠
        int tempImgW, tempImgH;
        boolean bFlag;
        switch ((byte) transform) {
            case Constant.TRANS_NONE:
            case Constant.TRANS_ROT180:
            case Constant.TRANS_MIRROR:
            case Constant.TRANS_MIRROR_ROT180:
                tempImgW = imgWidth;
                tempImgH = imgHeight;
                bFlag = true;
                break;
            case Constant.TRANS_ROT90:
            case Constant.TRANS_ROT270:
            case Constant.TRANS_MIRROR_ROT90:
            case Constant.TRANS_MIRROR_ROT270:
                tempImgW = imgHeight;
                tempImgH = imgWidth;
                bFlag = false;
                break;
            default:
                tempImgW = imgWidth;
                tempImgH = imgHeight;
                bFlag = true;
                break;
        }
        int n;
        if (direction == 1) {
            n = (borderLength % tempImgW == 0) ? borderLength / tempImgW : borderLength / tempImgW + 1; //需要填充的图片的数量
        } else {
            n = (borderLength % tempImgH == 0) ? borderLength / tempImgH : borderLength / tempImgH + 1; //需要填充的图片的数量
        }
        //填充
        for (int i = 1; i <= n; i++) {
            if (direction == 1) {
                if (startX + tempImgW >= iOriginalStartX + borderLength) {
                    tempBorderH = iOriginalStartX + borderLength - startX;
                } else {
                    tempBorderH = tempImgW;
                }
                if (bFlag) {
                    g.drawRegion(srcImg, imgStartX, imgStartY, tempBorderH, imgHeight, transform, startX, startY, 0);
                } else {
                    g.drawRegion(srcImg, imgStartX, imgStartY, imgWidth, tempBorderH, transform, startX, startY, 0);
                }
                startX = startX + tempBorderH;
            } else {
                if (startY + tempImgH >= iOriginalStartY + borderLength) {
                    tempBorderH = iOriginalStartY + borderLength - startY;
                } else {
                    tempBorderH = tempImgH;
                }
                if (bFlag) {
                    g.drawRegion(srcImg, imgStartX, imgStartY, imgWidth, tempBorderH, transform, startX, startY, 0);
                } else {
                    g.drawRegion(srcImg, imgStartX, imgStartY, tempBorderH, imgHeight, transform, startX, startY, 0);
                }
                startY = startY + tempBorderH;
            }
        }
    }
}
